package com.example.drug.controller;


import com.example.drug.domain.Staff;
import com.example.drug.service.StaffService;
import com.example.drug.util.AjaxResult;
import com.example.drug.util.BizException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 员工Controller
 *
 * @author [你的名字]
 * @date [具体日期]
 */

@RestController
@RequestMapping("/api/staff")
@Api(tags = "药店员工管理609余函池")
public class StaffController  {

    @Autowired
    private StaffService staffService;

    /**
     * 获取全部员工列表
     */
    @ApiOperation("获得全部员工列表609余函池")
    @GetMapping("/staffslist")
    public AjaxResult getAllStaffsList() {
        return AjaxResult.success(staffService.selectAllStaffs());
    }

    /**
     * 查询员工列表
     */
    @ApiOperation("查询员工列表609余函池")
    @ApiImplicitParam(name = "name", value = "员工姓名",
            dataType = "string", dataTypeClass = String.class)
    @GetMapping("/list")
    public AjaxResult list(@RequestParam(value = "name", required = false) String staffName) {
        List<Staff> list = staffService.selectStaffByParam(staffName);
        return AjaxResult.success(list);
    }

    /**
     * 获取员工详细信息
     */
    @ApiOperation("获取员工详细信息609余函池")
    @ApiImplicitParam(name = "id", value = "员工ID",
            required = true, dataType = "long", paramType = "path",
            dataTypeClass = Long.class)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        Staff staff = staffService.selectStaffById(id);
        if (staff == null || staff.getStaffId() == null) {
            throw new BizException(404, "员工 " + id + " 不存在");
        }
        return AjaxResult.success(staff);
    }

    /**
     * 新增员工
     */
    @ApiOperation("新增员工609余函池")
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Staff staff) {
        return AjaxResult.success(staffService.insertStaff(staff));
    }


    /**
     * 修改员工
     */
    @ApiOperation("修改员工609余函池")
    @PutMapping
    public AjaxResult edit(@RequestBody Staff staff) {
        return AjaxResult.success(staffService.updateStaff(staff));
    }

    /**
     * 删除员工
     */
    @ApiOperation("删除员工609余函池")
    @ApiImplicitParam(name = "id", value = "员工ID", required = true, dataType = "long", paramType = "path", dataTypeClass = Long.class)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return AjaxResult.success(staffService.deleteStaffById(id));
    }
}